Registered S3 method overwritten by 'tsibble':
method from
as_tibble.grouped_df dplyr
Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':
interval
The following objects are masked from 'package:base':
intersect, setdiff, union
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
library(forecast)
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Attaching package: 'forecast'
The following object is masked from 'package:yardstick':
accuracy
library(feasts)
Loading required package: fabletools
Attaching package: 'fabletools'
The following object is masked from 'package:yardstick':
accuracy
The following object is masked from 'package:parsnip':
null_model
The following objects are masked from 'package:infer':
generate, hypothesize
library(timetk)# Example: Cache la Poudre River at Mouth (USGS site 06752260)poudre_dailyflow <-readNWISdv(siteNumber ="06752260", # Download data from USGS for site 06752260parameterCd ="00060", # Parameter code 00060 = discharge in cfs)startDate ="2013-01-01", # Set the start dateendDate ="2023-12-31") |># Set the end daterenameNWISColumns() # Rename columns to standard names (e.g., "Flow", "Date"
# A tibble: 4,749 × 7
.model_id .model_desc .key .index .value .conf_lo .conf_hi
<int> <chr> <fct> <date> <dbl> <dbl> <dbl>
1 NA ACTUAL actual 2013-01-01 18.2 NA NA
2 NA ACTUAL actual 2013-01-02 17.9 NA NA
3 NA ACTUAL actual 2013-01-03 17.4 NA NA
4 NA ACTUAL actual 2013-01-04 17.4 NA NA
5 NA ACTUAL actual 2013-01-05 16.9 NA NA
6 NA ACTUAL actual 2013-01-06 15.5 NA NA
7 NA ACTUAL actual 2013-01-07 13.9 NA NA
8 NA ACTUAL actual 2013-01-08 13.5 NA NA
9 NA ACTUAL actual 2013-01-09 13.5 NA NA
10 NA ACTUAL actual 2013-01-10 17 NA NA
# ℹ 4,739 more rows
poudre_2024 <-readNWISdv(siteNumber ="06752260", # Download data from USGS for site 06752260parameterCd ="00060", # Parameter code 00060 = discharge in cfs)startDate ="2024-01-01", # Set the start dateendDate ="2024-12-31") |># Set the end daterenameNWISColumns() |># Rename columns to standard names (e.g., "Flow", "Date")mutate(Date =yearmonth(Date)) |># Convert daily Date values into a year-month format (e.g., "2023 Jan")group_by(Date) |># Group the data by the new monthly Datesummarise(Flow =mean(Flow)) # Calculate the average daily flow for each month
Warning: There was 1 warning in `filter()`.
ℹ In argument: `.model_desc == c("PROPHET", "ACTUAL")`.
Caused by warning in `.model_desc == c("PROPHET", "ACTUAL")`:
! longer object length is not a multiple of shorter object length
ggplot() +geom_line(data = forecast_plot, aes(x=Date, y = Flow, color ="Predicted")) +geom_line(data = poudre_2024, aes(x=Date, y = Flow, color ="Actual")) +theme_minimal() +labs(x ="Month",y ="Flow (Cubic Feet per Second)",color ="" )
The prediction of the prophet model is fairly accurate during times of relatively low flow, but severely overestimates peak flow in June. 2024 had exceptionally low average flow during the month of June compared to the rest of the years included in this model, which may explain why the model overestimated so much.
Call:
lm(formula = Predicted ~ Actual, data = poudre_join)
Residuals:
Min 1Q Median 3Q Max
-177.06 -74.31 -27.62 19.65 344.22
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -22.8991 53.0379 -0.432 0.675
Actual 2.8684 0.3979 7.209 2.9e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 141.2 on 10 degrees of freedom
Multiple R-squared: 0.8386, Adjusted R-squared: 0.8225
F-statistic: 51.97 on 1 and 10 DF, p-value: 2.896e-05
The R-squared value between the model predictions and the observed values is 0.8223, meaning that this model accounts for approximately 82% of the observed variance.
poudre_join %>%ggplot(aes(x = Actual, y = Predicted)) +geom_point()+geom_abline(linetype =1, color ="black") +geom_smooth(color ="red", method ="lm", formula = (y ~ x)) +theme_linedraw()